#include "dlg_bookau.h"
#include "ui_dlg_bookau.h"

Dlg_BookAU::Dlg_BookAU(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::Dlg_BookAU)
{
    ui->setupUi(this);

}

Dlg_BookAU::~Dlg_BookAU()
{
    delete ui;
}

void Dlg_BookAU::setType(int id)
{
    this->m_id = id;
    this->initView();
}

void Dlg_BookAU::initView()
{
    qDebug() << "bookid=" << this->m_id;

    if(this->m_id > 0){
        QMap<QString, QString> bookMap = SqlMgr::getInstance()->findBook(this->m_id);
        ui->le_name->setText(bookMap.value("name"));
        ui->le_cnt->setText(bookMap.value("cnt"));
        ui->le_price->setText(bookMap.value("price"));
        ui->cb_type1->setCurrentText(bookMap.value("type1"));
        ui->cb_type2->setCurrentText(bookMap.value("type2"));
        ui->cb_type3->setCurrentText(bookMap.value("type3"));
    }
}


void Dlg_BookAU::on_btn_ok_clicked()
{

    QMap<QString, QString> bookMap;
    bookMap.insert("name", ui->le_name->text());
    bookMap.insert("cnt", ui->le_cnt->text());
    bookMap.insert("type1", ui->cb_type1->currentText());
    bookMap.insert("type2", ui->cb_type2->currentText());
    bookMap.insert("type3", ui->cb_type3->currentText());
    bookMap.insert("price", ui->le_price->text());

    bool flag = false;

    //更新操作
    if(this->m_id > 0){
        bookMap.insert("bookid", QString::number(this->m_id));
        flag = SqlMgr::getInstance()->updateBook(bookMap);
    }
    //新增操作
    else{
        flag = SqlMgr::getInstance()->addBook(bookMap);
    }

    if(flag){
        this->close();
    }
}

void Dlg_BookAU::on_btn_cancel_clicked()
{
    this->hide();
}
